package com.example.fushuang.balabala.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.fushuang.balabala.R;
import com.example.fushuang.balabala.activities.AVActivity;
import com.example.fushuang.balabala.activities.WebLinkActivity;
import com.example.fushuang.balabala.adapter.RecommendRecycleAdapter;
import com.example.fushuang.balabala.client.ClientAPI;
import com.example.fushuang.balabala.model.RecommendEntry;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecommendFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {


    private SwipeRefreshLayout mRefreshLayout;
    private RecommendRecycleAdapter mAdapter;
    private List<RecommendEntry.ResultBean> mResult =new ArrayList<>();
    private static final String TAG = "RecommendFragment";

    public RecommendFragment() {
        // Required empty public constructor
    }

    @Override
    public String getFragmentTitle() {
        return "推荐";
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mAdapter = new RecommendRecycleAdapter(getContext(), mResult);

        //注册事件订阅者
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recommend, container, false);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recommend_recyclerView);
        mRefreshLayout = ((SwipeRefreshLayout) view.findViewById(R.id.recommend_refresh_layout));
        //通过布局管理器,对item排版,横竖瀑布流之类的
        LinearLayoutManager lm = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(lm);
        recyclerView.setAdapter(mAdapter);

        mRefreshLayout.setOnRefreshListener(this);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        ClientAPI.getRecommendListJSONAsync();
    }



    @Override
    public void onRefresh() {
        ClientAPI.getRecommendListJSONAsync();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveRecommendJson(RecommendEntry recommendEntry) {
        mRefreshLayout.setRefreshing(false);

        mResult.clear();
        mResult.addAll(recommendEntry.getResult());
        mAdapter.notifyDataSetChanged();
    }

    @Subscribe
    public void onRecommendBodyClickEvent(RecommendEntry.ResultBean.BodyBean bodyItem){
        // 根据goto 来进行显示不同的界面内容
        String aGoto = bodyItem.getGoto();
        // av 视频详情   推荐 区域
        // live 直播
        // weblink 网页   weblink , activity
        // bangumi_list 番剧
        Intent intent = null;
        switch (aGoto){
            case "av":
                intent = new Intent(getContext(), AVActivity.class);
                intent.putExtra("bodyItem",bodyItem);
                break;
            case "live":

                break;
            case "bangumi_list":

                break;

            case "weblink":
                intent=new Intent(getContext(), WebLinkActivity.class);
                intent.putExtra("bodyItem",bodyItem);
                break;

            default:
                break;
        }

        if(intent != null){
            startActivity(intent);
        }

    }

}
